package com.asialjim.wechat.official.menu;

import com.asialjim.wechat.apiservice.enumeration.WeChatApiReturnEnumeration;
import com.asialjim.wechat.apiservice.exception.WeChatApiException;
import com.asialjim.wechat.utils.JsonUtils;
import com.asialjim.wechat.utils.MapUtils;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <h2><em>ASIAL JIM JAVA DOC</em></h2><hr/>
 * <h3>CLASS DESCRIPTION</h3>
 * <p> The Custom Menus for WE CHAT Official Account <a href="https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.html">OFFICIAL DOCUMENT</a>
 * <p> 微信公众号自定义菜单 <a href="https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.html">微信官方文档传送门</a>
 * <p> Copyright &copy; ztzh inc Ztzh Co., LTD<hr/>
 * @author Asial Jim &nbsp;&nbsp;<span>Email: zengyz0128@zsmarter.com &nbsp;&nbsp; asialjim@hotmail.com</span>
 * @since 2020/6/10 11:18 &nbsp; JDK 1.8
 * @version 1.0.0
 */
@Data
@Slf4j
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper=false)
public class WeChatMenu implements Serializable {
	private static final long serialVersionUID = -6097152363376401656L;
	
	/**
     * 一级菜单
     */
    protected List<WeChatMenuButton> button;

    /**
     * 个性化菜单匹配规则
     */
    @JsonProperty(value = "matchrule")
    protected WeChatMenuMatchRule matchRule;

    /**
     * <h3>FIELD DESCRIPTION</h3>
     * the response for get we-chat official menu config and there is conditional menu needed
     */
    protected Integer menuid;

    /**
     * 一级菜单个数检查，数量为1-3个
     */
    public void buttonNumberCheck(){
        if (button == null)return;
        if (button.size() >3) WeChatApiException.cast(WeChatApiReturnEnumeration.CODE_40016);
        for (WeChatMenuButton weChatMenuButton : button) {
            var subButtons = weChatMenuButton.getSubButtons();
            if (Objects.isNull(subButtons)) return;
            // 二级菜单检查，不能超过五个二级菜单
            if (subButtons.size()>5)WeChatApiException.cast(WeChatApiReturnEnumeration.CODE_40023);
            log.debug("菜单具有二级菜单");
        }
    }

//    public static void main(String[] args) {
//        String s = "{\n" +
//                "     \"button\":[\n" +
//                "     {\t\n" +
//                "          \"type\":\"click\",\n" +
//                "          \"name\":\"今日歌曲\",\n" +
//                "          \"key\":\"V1001_TODAY_MUSIC\"\n" +
//                "      },\n" +
//                "      {\n" +
//                "           \"name\":\"菜单\",\n" +
//                "           \"sub_button\":[\n" +
//                "           {\t\n" +
//                "               \"type\":\"view\",\n" +
//                "               \"name\":\"搜索\",\n" +
//                "               \"url\":\"http://www.soso.com/\"\n" +
//                "            },\n" +
//                "            {\n" +
//                "                 \"type\":\"miniprogram\",\n" +
//                "                 \"name\":\"wxa\",\n" +
//                "                 \"url\":\"http://mp.weixin.qq.com\",\n" +
//                "                 \"appid\":\"wx286b93c14bbf93aa\",\n" +
//                "                 \"pagepath\":\"pages/lunar/index\"\n" +
//                "             },\n" +
//                "            {\n" +
//                "               \"type\":\"click\",\n" +
//                "               \"name\":\"赞一下我们\",\n" +
//                "               \"key\":\"V1001_GOOD\"\n" +
//                "            }]\n" +
//                "       }]\n" +
//                " }";
//
//
//    }
}
